package cn.zswltech.gruul.common.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author: Jason 2018/7/19
 */
public class DateUtils {
    public static final long MI_SEC_IN_DAY = 86400000L;
    public static final String DEFAULT_DATE_FMT = "yyyy-MM-dd HH:mm:ss";

    public static Date currentDate() {
        return new Date();
    }

    public static int diffDays(Date date1, Date date2) {
        return date2 != null && date1 != null ? (int) ((date1.getTime() - date2.getTime()) / 86400000L) : 0;
    }

    public static String formatDate(Object date, String fmt) {
        if (date == null) {
            return null;
        } else if (!(date instanceof Date)) {
            return date.toString();
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(fmt);
            return sdf.format(date);
        }
    }

    public static Date parseDate(String date) {
        return parseDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date parseDate(String date, String fmt) {
        if (date == null) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(fmt);

            try {
                return sdf.parse(date);
            } catch (ParseException var4) {
                return null;
            }
        }
    }

    public static String formatNow(String fmt) {
        return formatDate(new Date(), fmt);
    }

    public static String formatFullDate(Object date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatDateYMD(Object date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    public static Date addMinute(Date date, int minute) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(12, minute);
            return cal.getTime();
        }
    }

    public static Date addSecond(Date date, int second) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(13, second);
            return cal.getTime();
        }
    }

    public static Date addHour(Date date, int hour) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(11, hour);
            return cal.getTime();
        }
    }

    public static Date addDay(Date date, int days) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(6, days);
            return cal.getTime();
        }
    }

    public static Date addMonth(Date date, int months) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(2, months);
            return cal.getTime();
        }
    }

    public static Calendar toCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }

    public static boolean isSameDay(Date date1, Date date2) {
        return isSameDay(toCalendar(date1), toCalendar(date2));
    }

    public static Date getDayYMD(Date date) {
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.set(11, 0);
            cal.set(12, 0);
            cal.set(13, 0);
            cal.set(14, 0);
            return cal.getTime();
        }
    }

    public static int getHour(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(11);
    }

    public static int getMinute(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(12);
    }

    public static int getSecond(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(13);
    }

    public static boolean isDateAfter(Date d1, Date d2) {
        return d1.getTime() > d2.getTime();
    }

    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        int year1 = cal1.get(1);
        int day1 = cal1.get(6);
        int year2 = cal2.get(1);
        int day2 = cal2.get(6);
        return year1 != year2 ? false : day1 == day2;
    }

    public static boolean validateString(String dateString, String dateFormat) {
        if (StringUtils.isNotEmpty(dateString) && StringUtils.isNotEmpty(dateFormat)) {
            Pattern pattern = Pattern.compile(dateFormat);
            Matcher matcher = pattern.matcher(dateString);
            return matcher.find();
        } else {
            return false;
        }
    }
}
